package com.ruoyi.dataBase.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.dataBase.domain.IndustrialFunctDept;
import com.ruoyi.dataBase.domain.IndustrialTransportProject;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dataBase.domain.bo.IndustrialReportObjBo;
import com.ruoyi.dataBase.domain.vo.IndustrialReportObjVo;
import com.ruoyi.dataBase.domain.IndustrialReportObj;
import com.ruoyi.dataBase.mapper.IndustrialReportObjMapper;
import com.ruoyi.dataBase.service.IIndustrialReportObjService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填报对象Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-07
 */
@RequiredArgsConstructor
@Service
public class IndustrialReportObjServiceImpl implements IIndustrialReportObjService {

    private final IndustrialReportObjMapper baseMapper;

    /**
     * 查询填报对象
     */
    @Override
    public IndustrialReportObjVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询填报对象列表
     */
    @Override
    public TableDataInfo<IndustrialReportObjVo> queryPageList(IndustrialReportObjBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialReportObj> lqw = buildQueryWrapper(bo);
        Page<IndustrialReportObjVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填报对象列表
     */
    @Override
    public List<IndustrialReportObjVo> queryList(IndustrialReportObjBo bo) {
        LambdaQueryWrapper<IndustrialReportObj> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialReportObj> buildQueryWrapper(IndustrialReportObjBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialReportObj> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), IndustrialReportObj::getCompany, bo.getCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyType()), IndustrialReportObj::getCompanyType, bo.getCompanyType());
        lqw.eq(StringUtils.isNotBlank(bo.getUnifiedSocialCode()), IndustrialReportObj::getUnifiedSocialCode, bo.getUnifiedSocialCode());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), IndustrialReportObj::getContact, bo.getContact());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), IndustrialReportObj::getPhone, bo.getPhone());
        lqw.eq(bo.getDataStatus() != null, IndustrialReportObj::getDataStatus, bo.getDataStatus());

        lqw.like(StringUtils.isNotBlank(bo.getCreateBy()), IndustrialReportObj::getCreateBy, bo.getCreateBy());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateBy()), IndustrialReportObj::getUpdateBy, bo.getUpdateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            IndustrialReportObj::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            IndustrialReportObj::getCreateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));

        return lqw;
    }

    /**
     * 新增填报对象
     */
    @Override
    public Boolean insertByBo(IndustrialReportObjBo bo) {
        IndustrialReportObj add = BeanUtil.toBean(bo, IndustrialReportObj.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改填报对象
     */
    @Override
    public Boolean updateByBo(IndustrialReportObjBo bo) {
        IndustrialReportObj update = BeanUtil.toBean(bo, IndustrialReportObj.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialReportObj entity){
        //TODO 做一些数据校验,如唯一约束
        if (entity == null) {
            throw new IllegalArgumentException("对象不能为空");
        }

        // 校验公司名称唯一
        LambdaQueryWrapper<IndustrialReportObj> companyQuery = new LambdaQueryWrapper<IndustrialReportObj>()
            .eq(IndustrialReportObj::getCompany, entity.getCompany());

        if (entity.getId() != null) {
            companyQuery.ne(IndustrialReportObj::getId, entity.getId());
        }

        long companyCount = baseMapper.selectCount(companyQuery);
        if (companyCount > 0) {
            throw new RuntimeException("填报对象已存在");
        }

        // 校验社会统一信用代码唯一
        LambdaQueryWrapper<IndustrialReportObj> creditCodeQuery = new LambdaQueryWrapper<IndustrialReportObj>()
            .eq(IndustrialReportObj::getContact, entity.getContact());

        if (entity.getId() != null) {
            creditCodeQuery.ne(IndustrialReportObj::getId, entity.getId());
        }

        long creditCodeCount = baseMapper.selectCount(creditCodeQuery);
        if (creditCodeCount > 0) {
            throw new RuntimeException("社会统一信用代码已存在");
        }
    }

    /**
     * 批量删除填报对象
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
